var chaoqis = [];

var upd_id;
var search_data = {page:1,limit:5,
    type:stitle.value,
    timerule:stitle.value};

loadData();
function showData(arr){
    list_div.innerHTML = "";
    if(arr.length==0){
        list_div.innerHTML = "暂无数据";
        return;
    }
    var allhtml = "";
    allhtml += "<table width='100%'><tr>" +
        "<th>超期类型</th><th>超期时间规则</th><th>创建时间</th>" +
        "<th>修改时间</th><th>操作</th></tr>";
    for(var i=arr.length-1;i>=0;i--){
        var a = arr[i];
        allhtml +=
            "<tr class='list'>" +
            "<td class='ntitle'>"+a.type+"</td>" +
            "<td class='ntitle'>"+a.timerule+"</td>" +
            "<td class='ntitle'>"+a.createtime+"</td>" +
            "<td class='ntitle'>"+a.modifytime+"</td>" +
            "<td class='ntitle'><a class='edit_a' onclick='upd("+a.id+")' href='javascript:;'>编辑</a>" +
            "<a class='del_a' onclick='del("+a.id+")' href='javascript:;'>删除</a></td>" +
            "</tr>";
    }
    allhtml += "</table>";
    list_div.innerHTML = allhtml;
}

function search(){
    search_data = {page:1,limit:5,
        type:stitle.value,
        timerule:stitle.value}
    loadData();
}


function loadData(){
    list_div.innerHTML = "";
    loading.style.display = "block" ;
    ajax("/find_all_chaoqi",search_data,function(data){
        chaoqis = data;
        showData(chaoqis);
        loading.style.display = "none" ;
    });
    ajax("/count_all_chaoqi",search_data,function(data){
        // console.log(data);  33
        data = parseInt(data);
        current_page.innerHTML = search_data.page;
        total_page.innerHTML =
            (data%search_data.limit==0?
                data/search_data.limit:
                Math.floor(data/search_data.limit)+1);
    });
}


function save(){
    ajax("/add_chaoqi",{
        type:type1.value,
        timerule:type2.value,

    },function(data){
        alert("添加成功！");
        loadData();
        add_div.style.display = "none";
        list_div.style.display = "block";
        save_btn.style.display = "none";
        add_btn.style.display = "block";
        return_btn.style.display = "none";
//        page_div.style.display="block";
    })

}

function del(id){
    if(!confirm("您确定要删除这个数据吗？")){
        return;
    }
    var req = new XMLHttpRequest();
    req.open("post","/del_chaoqi");
    req.setRequestHeader("content-Type","application/json;charset=utf-8");
    var data = {
        id:id
    }
    req.send(JSON.stringify(data));
    req.onreadystatechange = function(){
        if(req.readyState==4&&req.status==200){
            if(req.responseText.trim()=="1"){
                alert("删除成功！");
            }

            loadData();
        }
    }
}


function add(){
    add_div.style.display = "block";
    list_div.style.display = "none";
    save_btn.style.display = "block";
    add_btn.style.display = "none";
    return_btn.style.display = "block";
//        page_div.style.display="none";
}



function updsave(){
    ajax("/upd_chaoqi",{
        id:upd_id,
        type:type3.value,
        timerule:type4.value,
    },function(data){
        alert("修改成功！");
        loadData();
        upd_div.style.display = "none";
        list_div.style.display = "block";
        updsave_btn.style.display = "none";
        add_btn.style.display = "block";
        return_btn.style.display = "none";
//                     page_div.style.display="block";

    })
}

function upd(id){
    upd_id=id;
    if(confirm("您确定要编辑这个数据吗？")){
        upd_div.style.display = "block";
        list_div.style.display = "none";
        updsave_btn.style.display = "block";
        add_btn.style.display = "none";
        return_btn.style.display = "block";
//          page_div.style.display="none";
//         type.select()
    }
}



function returnMain(){
    add_div.style.display = "none";
    list_div.style.display = "block";
    save_btn.style.display = "none";
    add_btn.style.display = "block";
    return_btn.style.display = "none";
    updsave_btn.style.display = "none";
//     page_div.style.display="block";

}

function select_type1(type1){
    var s;
    if(type1=="无"){
        for(var i=1;i<=365;i++){
            s+="<option>"+i+"</option>";
        }
    }else if(type1=="轻度超期"){
        for(var i=366;i<=730;i++){
            s+="<option>"+i+"</option>";
        }
    }else if(type1=="中度超期"){
        for(var i=731;i<=1095;i++){
            s+="<option>"+i+"</option>";
        }
    }
    else{
        for(var i=1096;i<=1435;i++){
            s+="<option>"+i+"</option>";
        }
    }
    type2.innerHTML=s;
}



function select_type3(type3){
    var s;
    if(type3=="无"){
        for(var i=1;i<=365;i++){
            s+="<option>"+i+"</option>";
        }
    }else if(type3=="轻度超期"){
        for(var i=366;i<=730;i++){
            s+="<option>"+i+"</option>";
        }
    }else if(type3=="中度超期"){
        for(var i=731;i<=1095;i++){
            s+="<option>"+i+"</option>";
        }
    }
    else{
        for(var i=1096;i<=1435;i++){
            s+="<option>"+i+"</option>";
        }
    }
    type4.innerHTML=s;
}

function prev_page(){
    if(search_data.page<=parseInt(total_page.innerHTML)&&search_data.page!=1) {
        search_data.page--;
        loadData();
    }
}


function next_page(){
    if(search_data.page<parseInt(total_page.innerHTML)) {
        search_data.page++;
        loadData();
    }
}

function to_page(){
    var ip = parseInt(input_page.value);
    if(ip>=1&&ip<=parseInt(total_page.innerHTML)){
        search_data.page = ip;
        loadData();
    }
}